package com.woniu.admin.config;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class LoginInterceptor implements HandlerInterceptor {
    //    方法调用之前执行这个逻辑
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HttpSession session = request.getSession();
        // 检测session里面有没有loginName的值，如果没有则提示先登录
        String loginName = (String) session.getAttribute("userName");
        if (loginName != null) {
            return true;
        }
        //重定向
//        response.sendRedirect(request.getContextPath() + "/static/login.html");//重定向到登录页面

        //转发
        request.getRequestDispatcher(request.getContextPath() + "/login.html").forward(request, response);

        return false;

    }
}
